package com.comtime.usercenter;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.comtime.greendaoutils.DataBaseUtil;
import com.comtime.smartbox.BaseActivity;
import com.comtime.smartkilimanjaro.R;
import com.comtime.utils.MyConfig;
import com.comtime.utils.MySharedPreferences;
import com.comtime.utils.Util;
import com.comtime.view.MyDialog;
import com.comtime.view.MyProgressDialog;
import com.comyou.comyouhttp.ComyouHttpCallBack;
import com.comyou.comyouhttp.ComyouHttpClient;
import com.comyou.comyouhttp.ComyouHttpProgram;

public class DeleteChildActivity extends BaseActivity {

	TextView tv_phonenum;
	TextView tv_title;
	TextView tv_name;

	MySharedPreferences mySharedPreferences;
	DataBaseUtil dataBaseUtil;
	
	String child_phonenum="";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_delete_child);
		mySharedPreferences = MySharedPreferences.getInstance(this);
		dataBaseUtil = DataBaseUtil.getInstance(this);
		init();
	}


	void init() {
		tv_title = (TextView) findViewById(R.id.tv_title);
		tv_phonenum = (TextView) findViewById(R.id.tv_phonenum);
		tv_name = (TextView) findViewById(R.id.tv_name);

		Intent intent = getIntent();
		int position = intent.getIntExtra(
				AccountManagerActivity.CHILD_POSITION, 1);

		tv_title.setText(getText(R.string.normal_user).toString() + position);
		tv_phonenum.setText(child_phonenum);

	}

	public void BackAction(View v) {
		finish();
		overridePendingTransition(R.anim.enter_left, R.anim.exit_right);
	}

	/**
	 * 解除授权
	 * 
	 * @param v
	 */
	public void DeleteAction(View v) {
		showDeleteDialog(getText(R.string.sure_to_un_consent_authorization)
				.toString());
	}

	/**
	 * 删除信息
	 */
	void delete() {

		if (!Util.hasNetwork(DeleteChildActivity.this)) {
			showToast(getText(R.string.no_network).toString());
			return;
		}

		final MyProgressDialog progressDialog = new MyProgressDialog(
				DeleteChildActivity.this);
		progressDialog.setContent(getText(R.string.login_getting).toString());
		progressDialog.show();
		ComyouHttpProgram httpProgram = new ComyouHttpProgram();
		httpProgram.add("phoneNum", mySharedPreferences.getPhoneNum());
		httpProgram.add("subPhoneNum", tv_phonenum.getText().toString());
		ComyouHttpClient httpClient = new ComyouHttpClient(MyConfig.IP
				+ "deletesubaccount.php");
		httpClient.postAsync(httpProgram, new ComyouHttpCallBack() {

			@Override
			public void onResponse(String string) {
				progressDialog.dismiss();
				Log.e("tag", "string:" + string);
				try {
					JSONObject jsonObject = new JSONObject(string);
					int err = jsonObject.getInt("err");
					if (err == 0) {
						handler.sendEmptyMessage(1);
					} else {
						Message msg=new Message();
						switch (err) {
						case 1:
							msg.obj=getText(R.string.missing_parameter);
							break;
						case 2:
							msg.obj=getText(R.string.child_not_mainphone_child);
							break;
						case 3:
							msg.obj=getText(R.string.phonenum_not_main);
							break;

						default:
							msg.obj=getText(R.string.delete_child_fail);
							break;
						}
						msg.what=2;
						handler.sendMessage(msg);
					}

				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(String arg0, IOException arg1) {
				progressDialog.dismiss();
				Message msg = new Message();
				msg.what = 2;
				msg.obj = getText(R.string.delete_child_fail);
				handler.sendMessage(msg);
			}
		});
	}

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				showToast(getText(R.string.add_child_success).toString());
				finish();
				overridePendingTransition(R.anim.enter_left, R.anim.exit_right);
				break;
			case 2:
				String str=(String) msg.obj;
				showToast(str);
				break;
			default:
				break;
			}
		};
	};

	void showDeleteDialog(String str) {
		final MyDialog dialog = new MyDialog(DeleteChildActivity.this);
		dialog.setContent(str);
		dialog.getLeftButton().setText(getText(R.string.cancel));
		dialog.getRighrButton().setText(getText(R.string.ok));
		dialog.getLeftButton().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				dialog.dismiss();
			}
		});
		dialog.getRighrButton().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				dialog.dismiss();
				delete();

			}
		});
		dialog.show();
	}

	/**
	 * 自定义提示
	 * 
	 * @param str
	 */
	Toast toast = null;

	void showToast(String str) {
		if (toast != null) {
			toast.cancel();
		}
		LayoutInflater inflater = getLayoutInflater();
		View view = inflater.inflate(R.layout.toastshow, null);
		TextView textView = (TextView) view.findViewById(R.id.toastshow);
		textView.setText(str);
		toast = new Toast(this);
		double d = getResources().getDisplayMetrics().density;
		toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.CENTER, 0, 0);
		toast.setDuration(1500);
		toast.setView(view);
		toast.show();
	}
}
